package cn.rocky.pandacodegen.service;

import cn.rocky.pandacodegen.common.config.GeneratorConfig;
import cn.rocky.pandacodegen.common.enums.DbType;
import cn.rocky.pandacodegen.database.mysqlfolder.MySqLServiceImpl;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *  Sql Service 工厂类 ==> 也可以使用枚举实现
 *
 * @author Rocky
 * @date 2023/07/03
 */
public class SQLServiceFactory {

    /**
     * 服务配置类 map<Integer, SQLService> : <dbType, SQLService>
     */
    private static final Map<Integer, SQLService> SERVICE_CONFIG = new ConcurrentHashMap<>(16);

    static {
        SERVICE_CONFIG.put(DbType.MYSQL.getType(), new MySqLServiceImpl());
    }

    /**
     * 工厂模式
     *
     * @param generatorConfig 代码生成配置
     * @return {@code SQLService}
     */
    public static SQLService build(GeneratorConfig generatorConfig) {
        SQLService service = SERVICE_CONFIG.get(generatorConfig.getDbType());
        if (service == null) {
            throw new RuntimeException("本系统暂不支持该数据源(" + generatorConfig.getDriverClass() + ")");
        }
        return service;
    }
}
